package Multithreading.Thread.test.ThreadLocalAndSynchronized;

// todo Synchronized : 同步机制采用  “时间换空间”，只提供一份变量，让多个线程排队进行访问
/** *********                       ********** */
public class SynchronizedDemo {
    private String content;

    public SynchronizedDemo() {
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "SynchronizedDemo{" +
                "content='" + content + '\'' +
                '}';
    }

    public static void main(String[] args) {
        SynchronizedDemo synDemo = new SynchronizedDemo();
        for(int i = 0; i < 5; i++){
            new Thread(new Runnable(){
                @Override
                public void run() {
                    synchronized(SynchronizedDemo.class){
                        synDemo.setContent(Thread.currentThread().getName() + "的数据");
                        System.out.print("\t --- \t");
                        System.out.println(Thread.currentThread().getName() + "-->" + synDemo.getContent());
                    }
                }
            },"线程"+i).start();
        }
    }
}
